import assert from 'node:assert/strict'
import { test } from 'node:test'
import { isFetchable, prefixWithSlash } from '../lib/utils.js'

test('should add slash if needed', async t => {
  const expectations = [
    { input: '', output: '/' },
    { input: '/foobar', output: '/foobar' },
    { input: undefined, output: '' },
    { input: null, output: '' }
  ]

  for (const exp of expectations) {
    assert.equal(prefixWithSlash(exp.input), exp.output)
  }
})

test('isFetchable unit test', t => {
  const cases = [
    { label: 'empty application', params: {}, expected: false },
    { label: 'openapi application from file', params: { openapi: { file: '/to/file' } }, expected: false },
    {
      label: 'openapi+graphql application, openapi has url',
      params: { openapi: { url: 'http://application/opeanapi' }, graphql: true },
      expected: true
    },
    { label: 'openapi+graphql application with default', params: { graphql: true, openapi: true }, expected: true }
  ]

  for (const c of cases) {
    assert.equal(isFetchable(c.params), c.expected, c.label)
  }
})
